package com.ncp.servlet.registerlogin;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ncp.model.biz.registerlogin.adminLoginBiz;

@SuppressWarnings("serial")
public class adminLoginServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		this.doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		response.setCharacterEncoding("UTF-8");
		/*
		 * 判断是否登录成功！
		 */
		String msg = "";//捕获用户登录时可能发生的提示信息。
		int number = 1;//捕获用户错误输入密码的次数
		adminLoginBiz adminloginbiz=new adminLoginBiz();
		HttpSession session = request.getSession(true);
		String adminLoginName = request.getParameter("adminLoginName");
		String passWord = request.getParameter("passWord");

			int isadminresult = adminloginbiz.isAdmin(adminLoginName);
			/***检查是否为注册用户
			 * -1代表不是注册用户。
			 * 1代表用户是注册用户 ****/
			if(isadminresult==1){
				/*** 是管理员，则判断密码是否正确
				 * -1代表密码输入错误。
				 * 1代表密码输入正确。 ****/
				int checkuserresult =adminloginbiz.checkAdminPsd(adminLoginName, passWord);
				if(checkuserresult==1){
						session.setAttribute("admin", "true");
						response.sendRedirect("pages/admin/index.jsp");
				}else{
					session.setAttribute("admin","false");
					if(session.getAttribute("number")==null){
						 session.setAttribute("number",number);
					}else{
						   number = Integer.parseInt(session.getAttribute("number").toString());
						   number++;
						   session.setAttribute("number",number);
					   }
					//这里解决获得审批的用户密码输入次数为3次则退出系统的处理。
			        if(number==3){
			        	msg = "密码输入次数达到3次，系统自动退出！";
			        	out.print("<script language=javascript>alert('"+msg+"');top.window.opener=null;top.window.open('','_self');top.window.close();</script>");
			        }
			        else{
			        	msg = "密码输入错误！";
						out.print("<script language=javascript>alert('"+msg+"');history.go(-1)</script>");
			        }
				}
			}else{
				msg = "没有此管理员！";
				out.print("<script language=javascript>alert('"+msg+"');history.go(-1)</script>");
			}
	}
}
